#include <iostream>
#include "comm.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include<unistd.h>
#include <fcntl.h>

int main()
{
    umask(0);
    // 新建管道
    int n = mkfifo(FIFO_FILE, 0666);
    if (n < 0)
    {
        std::cerr << "创建管道失败" << std::endl;
        exit(1);
    }

    std::cout << "创建管道成功!" << std::endl;

    // 打开管道
    int f = open(FIFO_FILE,O_RDONLY);
    if(f<0)
    {
        std::cerr<<"打开管道失败!"<<std::endl;
        exit(1);
    }

     std::cout<<"打开管道成功"<<std::endl;
    //读取管道内数据
    while(1)
    {
        
        char buffer[1024];
        int ret=read(f,buffer,sizeof(buffer));
        if(ret>0)
        {
            buffer[ret]=0;
            std::cout<<"client say:"<<buffer<<std::endl;
        }
        else if(ret==0)
        {
            std::cout<<"client quit!"<<std::endl;
            return 1;
        }
        else{
            std::cerr<<"read cerror"<<std::endl;
        }
    }


    //关闭管道
    close(f);



    //销毁管道

    unlink(FIFO_FILE);



        return 0;
}
